# 管理所有策略
# - 分配各自的StrategyContext
# - 分发各个策略的初始化信息
# - 控制各个策略的初始化、运行和停止
# - StrategyManager不直接保留策略，而是将策略传给StrategyContext，仅保存StrategyContext

from SynStategyContext import SynStrategyContext


class StrategyManager:
    all_strategy_context = {}
    public_context = None

    def __init__(self):
        self.all_strategy_context = {}
        self.public_context = None

    def register_strategy(self, strategy):
        # 确认没有重名策略
        assert strategy.strategy_name not in self.all_strategy_context.keys()
        strategy_context = SynStrategyContext(strategy)
        strategy_context.public_context = self.public_context
        self.all_strategy_context[strategy_context.strategy_name] = strategy_context

    def inform_order_update(self, order_update):
        stg_context = self.all_strategy_context[order_update.strategy_name]
        stg_context.put_event({'type': 'OrderUpdate', 'data': order_update})

    def initialize(self):
        for key in self.all_strategy_context.keys():
            self.all_strategy_context[key].strategy.initialize()

    def start(self):
        for key in self.all_strategy_context.keys():
            self.all_strategy_context[key].start()

    def join(self):
        for key in self.all_strategy_context.keys():
            self.all_strategy_context[key].join()
